package com.zhiyume.service;

import com.zhiyume.dao.api.TArticleMapper;
import com.zhiyume.dao.entity.TArticle;
import com.zhiyume.dao.entity.TArticleCriteria;
import com.zhiyume.dao.entity.TArticleWithBLOBs;
import com.zhiyume.service.common.ConstantEnum;
import com.zhiyume.service.util.GenerateKeyUtil;
import com.zhiyume.service.util.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author jiaxz
 * @description 文章相关的增删改查
 * @Date 2017-09-28
 */
@Service
public class ArticleService {

    @Autowired
    private TArticleMapper tArticleMapper;

    /**
     * @param id
     * @return
     * @description 根据Id查询文章信息
     * add by jiaxz
     */
    public String findArticleById(long id) {
        TArticleCriteria example = new TArticleCriteria();
        TArticleCriteria.Criteria c = example.createCriteria();
        c.andIdEqualTo(id);
        List<TArticle> articles = tArticleMapper.selectByExample(example);
        if (null != articles && articles.size() > 0) {
            return JsonUtil.returnSuccessInfo(ConstantEnum.CONSTANT_SUCCESS.getErrType(), ConstantEnum.CONSTANT_SUCCESS.getErrMsg(), articles.get(0));
        }
        return JsonUtil.returnErrorInfo(ConstantEnum.CONSTANT_NO_DATA.getErrType(), ConstantEnum.CONSTANT_NO_DATA.getErrMsg());
    }

    /**
     * @param title
     * @return
     * @descrition 根据用户名模糊查询文章信息
     * add by jiaxz
     */
    public String findArticleByTitle(String title) {

        TArticleCriteria example = new TArticleCriteria();
        TArticleCriteria.Criteria c = example.createCriteria();
        c.andTitleLike("%" + title + "%");
        List<TArticle> articles = tArticleMapper.selectByExample(example);
        if (null != articles && articles.size() > 0) {
            return JsonUtil.returnSuccessInfo(ConstantEnum.CONSTANT_SUCCESS.getErrType(), ConstantEnum.CONSTANT_SUCCESS.getErrMsg(), articles);
        }
        return JsonUtil.returnErrorInfo(ConstantEnum.CONSTANT_NO_DATA.getErrType(), ConstantEnum.CONSTANT_NO_DATA.getErrMsg());
    }

    /**
     * @param tArticle
     * @return
     * @descrition 增加文章
     * add by jiaxz
     */
    public String addArticle(TArticleWithBLOBs tArticle) {
        long id = GenerateKeyUtil.generationPK();
        tArticle.setId(id);
        tArticleMapper.insert(tArticle);
        return JsonUtil.returnSuccessInfo(ConstantEnum.CONSTANT_SUCCESS.getErrType(), ConstantEnum.CONSTANT_SUCCESS.getErrMsg(), tArticle);
    }

    /**
     * @param tArticle
     * @return
     * @descrition 修改文章
     * add by jiaxz
     */
    public String updateArticle(TArticle tArticle) {
        TArticleCriteria example = new TArticleCriteria();
        TArticleCriteria.Criteria c = example.createCriteria();
        c.andIdEqualTo(tArticle.getId());
        tArticleMapper.updateByExample(tArticle, example);
        return JsonUtil.returnSuccessInfo(ConstantEnum.CONSTANT_SUCCESS.getErrType(), ConstantEnum.CONSTANT_SUCCESS.getErrMsg(), tArticle);
    }

    /**
     * @description 根据Id删除文章
     * @param id
     * @return
     */
    public String deleteArticleById(long id) {
        TArticleCriteria example = new TArticleCriteria();
        TArticleCriteria.Criteria c = example.createCriteria();
        c.andIdEqualTo(id);
        tArticleMapper.deleteByExample(example);
        return JsonUtil.returnSuccessInfo(ConstantEnum.CONSTANT_SUCCESS.getErrType(), ConstantEnum.CONSTANT_SUCCESS.getErrMsg());
    }
    /**
     * @description 根据title删除文章
     * @param title
     * @return
     */
    public String deleteArticleByTitle(String title) {
        TArticleCriteria example = new TArticleCriteria();
        TArticleCriteria.Criteria c = example.createCriteria();
        c.andTitleEqualTo(title);
        tArticleMapper.deleteByExample(example);
        return JsonUtil.returnSuccessInfo(ConstantEnum.CONSTANT_SUCCESS.getErrType(), ConstantEnum.CONSTANT_SUCCESS.getErrMsg());
    }
}
